﻿using Engine;
using Engine.Graphics;
using Game;
using Mekiasm.Common;
using System.Collections.Generic;

namespace Mekiasm
{
    public class FlashLight : ComplexModelItem, Interface.IChargeAbleItem
    {
        public static int Index;

        public static bool Switch;

        public FlashLight() : base(new ModelItem[] {
            new ModelItem("cube.020", "FlashLight"), new ModelItem("cube.019", "FlashLight"), new ModelItem("cube.018", "FlashLight"),
            new ModelItem("cube.017", "FlashLight"), new ModelItem("cube.016", "FlashLight"), new ModelItem("cube.015", "FlashLight"),
            new ModelItem("cube.014", "FlashLight"), new ModelItem("cube.013", "FlashLight"), new ModelItem("cube.012", "FlashLight"),
            new ModelItem("cube.011", "FlashLight"), new ModelItem("cube.010", "FlashLight"), new ModelItem("cube.009", "FlashLight"),
            new ModelItem("cube.008", "FlashLight"), new ModelItem("cube.007", "FlashLight"), new ModelItem("cube.007", "FlashLight"),
            new ModelItem("cube.006", "FlashLight"), new ModelItem("cube.006", "FlashLight"), new ModelItem("cube.005", "FlashLight"),
            new ModelItem("cube.005", "FlashLight"), new ModelItem("cube.004", "FlashLight"), new ModelItem("cube.003", "FlashLight"),
            new ModelItem("cube.002", "FlashLight"), new ModelItem("cube.002", "FlashLight"), new ModelItem("cube.001", "FlashLight"),
            new ModelItem("cube.001", "FlashLight"), new ModelItem("cube.000", "FlashLight"), new ModelItem("cube", "FlashLight")
        }, "FlashLight", "手电筒")
        {

        }
        public override void DrawBlock(PrimitivesRenderer3D primitivesRenderer, int value, Color color, float size, ref Matrix matrix, DrawBlockEnvironmentData environmentData)
        {
            if (MekModLoader.lightOn == 1f)
            {
                environmentData.Light = 15;
            }
            base.DrawBlock(primitivesRenderer, value, color, size, ref matrix, environmentData);
        }
        public override bool OnUse(Ray3 aim, ComponentMiner componentMiner, TerrainRaycastResult? raycast)
        {
            Switch = !Switch;
            if (Switch)
            {
                DisplayMsg(componentMiner.ComponentPlayer, "打开");

            }
            else
            {
                DisplayMsg(componentMiner.ComponentPlayer, "关闭");
            }
            return true;
        }
        private Vector3 InHandRotation = new Vector3(90f, 0f, 0f);
        private Vector3 InHandOffset = new Vector3(-0.1f, 0.1f, -0.1f);
        private Vector3 FirstPersionInHandOffset = new Vector3(0.5f, -0.5f, -0.6f);

        public ComponentEnergyMachine.FuelType ChargeFuelType => ComponentEnergyMachine.FuelType.Electric;

        public long MaxAmount => GetMaxPower();

        public override Vector3 GetFirstPersonOffset(int value)
        {
            return FirstPersionInHandOffset;
        }
        public override Vector3 GetInHandOffset(int value)
        {
            return InHandOffset;
        }
        public override Vector3 GetInHandRotation(int value)
        {
            return InHandRotation;
        }
        public override bool IsCrushWhenNoPower()
        {
            return false;
        }

        public override int GetMaxPower()
        {
            return 10240;
        }
        public static Point3? Last;
        public override void OnHand(ComponentPlayer player, int value)
        {
            SaveItemSystem.GetBlockData(value, out SaveItem saveItem);
            if (saveItem != null && saveItem.fuelAmount > 0 && Switch)
            {
                SaveItemSystem.ChangePower(player.ComponentMiner.Inventory, player.ComponentMiner.Inventory.ActiveSlotIndex, -1);
                var p = Terrain.ToCell(player.ComponentBody.Position);
                if (Last == null || p != Last.Value)
                {
                    player.m_subsystemTerrain.TerrainUpdater.DowngradeChunkNeighborhoodState(Terrain.ToChunk(p.X, p.Z), 1, TerrainChunkState.InvalidLight, true);
                    Last = p;
                }
            }
            else if (!Switch && Last.HasValue)
            {
                OnHandOut(player, 0, 0);
            }
        }
        public override void OnHandOut(ComponentPlayer player, int oldValue, int newValue)
        {
            var p = Terrain.ToCell(player.ComponentBody.Position);
            player.m_subsystemTerrain.TerrainUpdater.DowngradeChunkNeighborhoodState(Terrain.ToChunk(p.X, p.Z), 1, TerrainChunkState.InvalidLight, true);
            Last = null;
        }


        public override float GetBlockHealth(int value)
        {
            SaveItemSystem.GetBlockData(value, out var d);
            if (d == null) return -1f;
            else return GetCurrentAmount(value) / (float)GetMaxPower();
        }


        public long GetCurrentAmount(int value)
        {
            SaveItemSystem.GetBlockData(value, out var d);
            if (d == null) return 0;
            else return d.fuelAmount;
        }

        public int SetCurrentAmount(int value, int fuelValue, long varAmount)
        {
            return SaveItemSystem.ChangePower(value, varAmount, fuelValue);
        }

        public int GetChargeFuelValue(int value)
        {
            return 0;
        }

        public bool CanAcceptFuel(int value)
        {
            return true;
        }
    }
}
